package com.zcurd.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.plugin.QuartzKey;
import com.zcurd.common.plugin.QuartzKit;
import com.zcurd.common.plugin.job.JobState;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.ext.render.csv.CsvRender;
import com.zcurd.model.SysJob;

/**
 * 系统操作日志
 * @author xpg
 */
public class SysJobController extends BaseController {
	
	public void listPage() {
		
		setAttr("job_type", SysJob.me.getJobType());		
		setAttr("run_status", SysJob.me.getRunStatus());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}		
		List<Record> list = DBTool.findByMultProperties("sys_Job", properties, symbols, values, orderBy, getPager());
		Map<String, Object> jobtype = SysJob.me.getJobType();		
		Map<String, Object> runStatus = SysJob.me.getRunStatus();
		for(Record record : list) {
			String job_type = "job_type";			
			String job_status = "run_status";
			if(jobtype.get(record.get(job_type).toString()) != null||runStatus.get(record.get(job_status).toString()) != null) {
				record.set(job_type, jobtype.get(record.get(job_type).toString()));				
				record.set(job_status, runStatus.get(record.get(job_status).toString()));
			}
		}		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("sys_job", properties, symbols, values)
		);
	}
	//作业任务启停
	public void useAndforb() {
			Integer[] ids = getParaValuesToInt("id[]");
			for (Integer id : ids) {
				SysJob.me.useAndforb(id);
			}	
			addOpLog("[作业管理] 启停任务");
			renderSuccess();
		}
		//启动作业任务
	public void startJob() {
			Integer[] ids = getParaValuesToInt("id[]");
			for (Integer id : ids) {
				SysJob sysjob =SysJob.me.findById(id);
				QuartzKey quartzKey = new QuartzKey(sysjob.getInt("id"),sysjob.getStr("job_name"),sysjob.getStr("job_group"));
				QuartzKit.getJob(quartzKey).start(true);
				sysjob.set("run_status",JobState.STARTED.toString()).update();
			}		
			addOpLog("[作业管理] 启动任务");
			renderSuccess();
		}
	//恢复作业任务
	public void resumeJob() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			SysJob sysjob =SysJob.me.findById(id);
			QuartzKey quartzKey = new QuartzKey(sysjob.getInt("id"),sysjob.getStr("job_name"),sysjob.getStr("job_group"));
			QuartzKit.resumeJob(quartzKey);
			sysjob.set("run_status",JobState.RESUMED.toString()).update();
		}
		addOpLog("[作业管理] 恢复任务");
		renderSuccess();
	}
	
	//停止作业任务
	public void stopJob() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			SysJob sysjob =SysJob.me.findById(id);
			QuartzKey quartzKey = new QuartzKey(sysjob.getInt("id"),sysjob.getStr("job_name"),sysjob.getStr("job_group"));
			QuartzKit.stopJob(quartzKey);
			sysjob.set("run_status",JobState.STOPED.toString()).update();
		}		
		addOpLog("[作业管理] 停止任务");
		renderSuccess();
	}
	//暂停作业任务
	public void pauseJob() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			SysJob sysjob =SysJob.me.findById(id);
			QuartzKey quartzKey = new QuartzKey(sysjob.getInt("id"),sysjob.getStr("job_name"),sysjob.getStr("job_group"));
			QuartzKit.pauseJob(quartzKey);
			sysjob.set("run_status",JobState.PAUSED.toString()).update();
		}
		addOpLog("[作业管理] 暂停任务");
		renderSuccess();
	}
	public void addPage() {
		render("add.html");
	}
	
	public void add() {
		if(getModel(SysJob.class, "model").set("run_status",JobState.INITED).save()) {
			addOpLog("[作业管理] 增加");
			renderSuccess();
		}else {
			renderFailed();
		}
	}
	
	public void updatePage() {
		setAttr("model", SysJob.me.findById(getParaToInt("id")));
		render("update.html");
	}
	
	public void update() {
		if(getModel(SysJob.class, "model").update()) {
			addOpLog("[作业管理] 修改");
			renderSuccess();
		}else {
			renderFailed();
		}
	}
	
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			SysJob.me.deleteById(id);
		}
		addOpLog("[作业管理] 删除");
		renderSuccess();
	}
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("sys_job", properties, symbols, values);
		Map<String, Object> jobtype = SysJob.me.getJobType();		
		Map<String, Object> runStatus = SysJob.me.getRunStatus();
		for(Record record : list) {
			String job_type = "job_type";			
			String job_status = "run_status";
			if(jobtype.get(record.get(job_type).toString()) != null||runStatus.get(record.get(job_status).toString()) != null) {
				record.set(job_type, jobtype.get(record.get(job_type).toString()));				
				record.set(job_status, runStatus.get(record.get(job_status).toString()));
			}
		}			
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("作业ID");
		headers.add("作业名称");
		headers.add("作业实现类");
		headers.add("作业组");
		headers.add("作业时间规则（周期任务）");		
		headers.add("作业类型");
		headers.add("作业描述");
		headers.add("执行时间（单次任务）");
		clomuns.add("id");
		clomuns.add("job_name");
		clomuns.add("job_class");
		clomuns.add("job_group");
		clomuns.add("job_cron");
		clomuns.add("job_type");
		clomuns.add("job_desc");
		clomuns.add("start_time");
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("作业清单");
		
		addOpLog("[作业清单] 导出cvs");
		render(csvRender);
	}
}
